package com.edgclub.edg.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.edgclub.edg.R;
import com.edgclub.edg.adapter.AdvertImagePagerAdapter;
import com.edgclub.edg.constant.BBConstant;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.edgclub.edg.model.Banner;
import com.edgclub.edg.uc.autoscrollviewpager.AutoScrollViewPager;
import com.squareup.otto.ThreadEnforcer;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2016/6/28.
 */
public class BannerComponent {
    private Activity mAct;
    private List<Banner> banners;
    private LinearLayout viewGroup;
    private int preEnablePositon = 0; // 前一个被选中的点的索引位置 默认情况下为0
    private AutoScrollViewPager viewPager;

    private AdvertImagePagerAdapter advertImagePagerAdapter;


    public void init(final Activity mAct, final AutoScrollViewPager viewPager, LinearLayout viewGroup) {
        this.mAct = mAct;
        this.viewPager = viewPager;
        this.viewGroup = viewGroup;
        banners = new ArrayList<>();
        initdata();
    }

    private void initdata() {
        advertImagePagerAdapter=new AdvertImagePagerAdapter(mAct);
        advertImagePagerAdapter.setImageIdList(banners);
        viewPager.setAdapter(advertImagePagerAdapter);
        viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
        viewPager.setInterval(5000);
        advertImagePagerAdapter.setImageOnclickListener(new AdvertImagePagerAdapter.ImageOnclickListener() {
            @Override
            public void onImageclick(Banner banner) {
                try {
                    //统计点击事件
                    HashMap<String,String> map = new HashMap<String,String>();
                    map.put("tid",banner.getId());
                    map.put("title",banner.getTitle());
                    MobclickAgent.onEvent(mAct, "EC_FIND_BANNER", map);

                    String protocol = banner.getProtocol();
                    String msg = protocol.substring(protocol.indexOf("//")+2);
                    String key = msg.substring(0,msg.indexOf("/"));
                    String value = msg.substring(msg.indexOf("/")+1);

                    if(key.equals(BBConstant.BANNER_JUMP_TYPE_URL)){
                        Intent i = new Intent(mAct,GzWebViewActivity.class);
                        i.putExtra("url", value);
                         i.putExtra("title", banner.getTitle());
                        mAct.startActivity(i);
                    }else if(key.equals(BBConstant.BANNER_JUMP_TYPE_BB)){
                        Intent intent = new Intent(mAct,GzTopicDetailActivity.class);
                        intent.putExtra("topicId", value);
                        mAct.startActivity(intent);

                    }else if(protocol.contains(BBConstant.BANNER_JUMP_TYPE_TAOBAO1) || protocol.contains(BBConstant.BANNER_JUMP_TYPE_TAOBAO2)){
                        //跳转商城
                        HashMap<String, Object> hashMap = new HashMap<String, Object>();
                        hashMap.put("event", BroadCastEvent.JUMPTAOBAO);
                        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
                                ThreadEnforcer.MAIN).post(hashMap);

                    }else if(key.equals( BBConstant.BANNER_JUMP_TYPE_GROUP)){
                        Intent intent=new Intent(mAct, GroupDetailsActivity.class);
                        intent.putExtra("groupid",value);
                        intent.putExtra("groupname","群组");
                        mAct.startActivity(intent);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        });
    }
    /**
     * 初始化小圆点
     */
    private void initDot() {
        viewGroup.removeAllViews();
        preEnablePositon = 0;
        LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < banners.size(); i++) {
            if(mAct==null)return;
            ImageView dotImageView = new ImageView(mAct);
            dotImageView.setId(i);
            dotImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            dotImageView.setLayoutParams(localLayoutParams);
            dotImageView.setPadding(5, 5, 5, 5);
            dotImageView.setImageResource(R.drawable.gallery_page_indicator_selector);

            if (i == 0) {
                dotImageView.setEnabled(true);//第一次选上第一个
            }else{
                dotImageView.setEnabled(false);
            }
            viewGroup.addView(dotImageView);
        }
    }


    public class  MyOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            // 取余后的索引
            int newPositon = position % banners.size();

            // 把上一个点设置为被选中
            viewGroup.getChildAt(preEnablePositon).setEnabled(false);

            // 根据索引设置那个点被选中
            viewGroup.getChildAt(newPositon).setEnabled(true);

            preEnablePositon = newPositon;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    public void stopAutoScroll(){
        viewPager.stopAutoScroll();
    }
    public void startAutoScroll(){
        viewPager.startAutoScroll();
    }


    public List<Banner> getBanners() {
        return banners;
    }

    public void setBanners(List<Banner> banners) {
        this.banners = banners;
        advertImagePagerAdapter.setImageIdList(banners);
        advertImagePagerAdapter.notifyDataSetChanged();
        initDot();
        // 初始化viewpager的默认position.MAX_value的一半

//        viewPager.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 %banners.size());
        viewPager.stopAutoScroll();
        viewPager.startAutoScroll();
    }
}
